/*  TURBOC replacement for utimes.  Sets file time to time set in
    'times' - but only uses modtime.  If 'times' is NULL then sets
    file's time to now. Returns 0 on success, -1 on failure (can't
    open the file).
*/

/*
    Contact info:
    bhepple@freeshell.org
    http://bhepple.freeshell.org

    Copyright (C) 1999-2008 Bob Hepple

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/

#ifdef SM_TURBOC
#include <time.h>
#include <sys/types.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>

struct utimbuf {
    time_t	actime;
    time_t	modtime;
};

int utimes(filename, times)
    char		*filename;
    struct utimbuf	*times;
{
    int			f;
    struct ftime	ftimeb;
    time_t		time_to_set;
    struct tm		*tmbuf;
    int			retval;

    f = open(filename, O_RDONLY);
    if (f < 0)
	return(-1);

    if (times)
	time_to_set = times->modtime;
    else
	time_to_set = time(NULL);

    tmbuf = localtime(&time_to_set);

    ftimeb.ft_tsec =  tmbuf->tm_sec / 2;
    ftimeb.ft_min =   tmbuf->tm_min;
    ftimeb.ft_hour =  tmbuf->tm_hour;
    ftimeb.ft_day =   tmbuf->tm_mday;
    ftimeb.ft_month = tmbuf->tm_mon;
    ftimeb.ft_year =  tmbuf->tm_year + 1900 - 1980;
    
    retval = setftime(f, &ftimeb);
    close(f);
    return(retval);
}

#ifdef TEST
/* A simple touch program ... */
main(argc, argv)
    int		argc;
    char	*argv[];
{
    int		argd;
    char	*filename;

    for (argd = 1; (filename = argv[argd]) != NULL; argd++)
	utimes(filename, NULL);
}
#endif /* TEST */

#endif /* SM_TURBOC */


/* For emacs:
 * Local Variables:
 * tab-width: 8
 * End:
 */
